package br.com.translog.gui.cliente;

import br.com.translog.fachada.FachadaTranslog;
import br.com.translog.gui.outro.FrmMenuPrincipal;
import br.com.translog.modelo.Cliente;
import br.com.translog.modelo.Logradouro;
import br.com.translog.modelo.PessoaJuridica;
import javax.swing.JOptionPane;

public class FrmClienteExcluir extends javax.swing.JFrame {

    /**
     * Creates new form FrmClienteExcluir
     */
    public FrmClienteExcluir() {
        initComponents();
        setLocationRelativeTo(null);
    }
    
    //Instanciando os objetos que serao utilizados...
    Cliente c = null;
    PessoaJuridica pj = null;
    Logradouro l = null;

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlClienteExcluir = new javax.swing.JPanel();
        lblExcluiCliente = new javax.swing.JLabel();
        lblCNPJ = new javax.swing.JLabel();
        lblCodigoCliente = new javax.swing.JLabel();
        lblRazaoSocial = new javax.swing.JLabel();
        lblNomeFantasia = new javax.swing.JLabel();
        txtCNPJ = new javax.swing.JTextField();
        txtCodigoCliente = new javax.swing.JTextField();
        txtRazaoSocial = new javax.swing.JTextField();
        txtNomeFantasia = new javax.swing.JTextField();
        btnPesquisar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnlClienteExcluir.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlClienteExcluir.setMaximumSize(new java.awt.Dimension(946, 278));
        pnlClienteExcluir.setMinimumSize(new java.awt.Dimension(946, 278));

        lblExcluiCliente.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        lblExcluiCliente.setText("EXCLUIR CLIENTE");
        lblExcluiCliente.setName(""); // NOI18N

        lblCNPJ.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblCNPJ.setText("CNPJ");

        lblCodigoCliente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblCodigoCliente.setText("Código Cliente");

        lblRazaoSocial.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblRazaoSocial.setText("Razão social");

        lblNomeFantasia.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNomeFantasia.setText("Nome Fantasia");

        txtCodigoCliente.setEditable(false);

        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlClienteExcluirLayout = new javax.swing.GroupLayout(pnlClienteExcluir);
        pnlClienteExcluir.setLayout(pnlClienteExcluirLayout);
        pnlClienteExcluirLayout.setHorizontalGroup(
            pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlClienteExcluirLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlClienteExcluirLayout.createSequentialGroup()
                        .addComponent(lblRazaoSocial)
                        .addGap(18, 18, 18)
                        .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblExcluiCliente)
                            .addGroup(pnlClienteExcluirLayout.createSequentialGroup()
                                .addComponent(txtRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblCodigoCliente)
                                    .addComponent(lblNomeFantasia))))
                        .addGap(18, 18, 18)
                        .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNomeFantasia)
                            .addGroup(pnlClienteExcluirLayout.createSequentialGroup()
                                .addComponent(txtCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 305, Short.MAX_VALUE))))
                    .addGroup(pnlClienteExcluirLayout.createSequentialGroup()
                        .addComponent(lblCNPJ)
                        .addGap(53, 53, 53)
                        .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(pnlClienteExcluirLayout.createSequentialGroup()
                .addGap(342, 342, 342)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlClienteExcluirLayout.setVerticalGroup(
            pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlClienteExcluirLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblExcluiCliente)
                .addGap(18, 18, 18)
                .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblCNPJ)
                        .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnPesquisar))
                    .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblCodigoCliente)
                        .addComponent(txtCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNomeFantasia)
                    .addComponent(txtNomeFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRazaoSocial))
                .addGap(18, 18, 18)
                .addGroup(pnlClienteExcluirLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluir)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlClienteExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlClienteExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed

      PessoaJuridica  pj;
        try {
            //Verifica se o campo de pesquisa nao esta em "branco"...
            if (!txtCNPJ.getText().equals("")) {
                pj = FachadaTranslog.getInstFachadaTranslog().consultaCliente(Integer.parseInt(txtCNPJ.getText()));
                if (c == null) {
                    JOptionPane.showMessageDialog(null, "Cliente não Encontrado!");
                } else {
                    //Setando os valores do objeto cliente nos JTextFields...
                    l = pj.getCliente().getCodigoLogradouro();
                    txtCNPJ.setText(String.valueOf(pj.getCnpj()));
                    txtNomeFantasia.setText(pj.getNomeFantasia());
                    txtRazaoSocial.setText(pj.getRazaoSocial());
                    
                }
                
            } else {
                JOptionPane.showMessageDialog(null, "Dados Obrigatórios não Informados!");
            }
            
        } catch (Exception e) {
        }
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed

        try {
            FachadaTranslog.getInstFachadaTranslog().excluiCliente(c, pj, l);
        } catch (Exception e) {
        }
            
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        new FrmMenuPrincipal().setVisible(true);
        this.dispose();
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmClienteExcluir.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmClienteExcluir.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmClienteExcluir.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmClienteExcluir.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmClienteExcluir().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JLabel lblCNPJ;
    private javax.swing.JLabel lblCodigoCliente;
    private javax.swing.JLabel lblExcluiCliente;
    private javax.swing.JLabel lblNomeFantasia;
    private javax.swing.JLabel lblRazaoSocial;
    private javax.swing.JPanel pnlClienteExcluir;
    private javax.swing.JTextField txtCNPJ;
    private javax.swing.JTextField txtCodigoCliente;
    private javax.swing.JTextField txtNomeFantasia;
    private javax.swing.JTextField txtRazaoSocial;
    // End of variables declaration//GEN-END:variables
}
